package br.net.atasacramental.reuniao.dao;
/**
 * @author root
 */
import br.net.atasacramental.reuniao.model.Reuniao;
import br.net.atasacramental.util.ConexaoDB;
import br.net.atasacramental.util.Paginacao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ReuniaoDAO {
	private String[][] ordemP = {{"momento", "desc"}};
	@SuppressWarnings("unchecked")
    public List<Reuniao> get(){
    	Date DtaI = new Date();
    	Date DtaF = new Date();
    	Long mes = 2678400000L;    
    	DtaI.setTime(DtaI.getTime()-mes*12);
    	DtaF.setTime(DtaF.getTime()+mes*6);
        //return (List<Reuniao>) ConexaoDB.listar("From Reuniao r where r.momento > '1357009199000' order by r.idReuniao desc");
    	return (List<Reuniao>) ConexaoDB.getBetween(Reuniao.class,true,1,100,"momento",DtaI,DtaF,ordemP);
    }
	@SuppressWarnings("unchecked")
    public List<Reuniao> getPaginado(Paginacao p){   
     	Map<String,Object> params = new HashMap<String, Object>();     
    	if(p.getOrderBy()!=null){
    		String[][] ordemP2 = {{p.getOrderBy(),p.getOrder()}};
    		ordemP = ordemP2;
    	}
    	if(!(p.getFilterValue()==(null)||p.getFilterBy().equals("idReuniao"))){
    		params.put(p.getFilterBy(), p.getFilterValue());
    	}
        //return (List<Reuniao>) ConexaoDB.listar("From Reuniao r where r.momento > '1357009199000' order by r.idReuniao desc");
    	return (List<Reuniao>) ConexaoDB.get(Reuniao.class,false,p.getInitial(),p.getMax(),params,ordemP);
    }
    public Reuniao getByIdReuniao(int idReuniao){
    	return (Reuniao) ConexaoDB.getById(Reuniao.class,idReuniao);
    }
    @SuppressWarnings("unchecked")
    public List<Reuniao> getByPeriodo(Date DtaF,Date DtaI){
     	DtaF.setTime(DtaF.getTime()+86399999L);
		return (List<Reuniao>) ConexaoDB.getBetween(Reuniao.class,true,1,100,"momento",DtaI,DtaF,ordemP);        
        
    } 
    @SuppressWarnings("unchecked")
    public List<Reuniao> getByPeriodoSemDiscursante(Date dtaI,Date dtaF){    	
    	dtaF.setTime(dtaF.getTime()+86399999L);
    	String dataz = "yyyy-MM-dd";
        SimpleDateFormat formatas = new SimpleDateFormat(dataz );
        String dataI = formatas.format(dtaI);
        String dataF = formatas.format(dtaF);        
        return (List<Reuniao>) ConexaoDB.listar("From Reuniao r where not exists ( from Discurso di where di.idReuniao = r.idReuniao  ) and r.momento between '"+dataI+"' and '"+dataF+"'  order by r.momento");
    }    
    public boolean insert(Reuniao memb){
        return ConexaoDB.insert(memb);
    }
    public boolean update(Reuniao memb){
        return ConexaoDB.update(memb);
    }
}